﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp_Binding.DataCollection
{
    public class DataSource:INotifyPropertyChanged
    {
        public List<AreaItem> _areas = new List<AreaItem>()
            {
                new AreaItem(){ Id = 1,Name = "四川",FatherId=-1,TypeId = 1 },
                new AreaItem(){ Id = 2,Name = "安徽",FatherId=-1,TypeId = 1 },
                new AreaItem(){ Id = 3,Name = "成都",FatherId=1,TypeId = 2 },
                new AreaItem(){ Id = 4,Name = "广元",FatherId=1,TypeId = 2 },
                new AreaItem(){ Id = 5,Name = "双流区",FatherId=3,TypeId = 3 },
                new AreaItem(){ Id = 6,Name = "高新区",FatherId=3,TypeId = 3 },
                new AreaItem(){ Id = 5,Name = "利州区",FatherId=4,TypeId = 3 },
                new AreaItem(){ Id = 6,Name = "元坝区",FatherId=4,TypeId = 3 },
                new AreaItem(){ Id = 7,Name = "合肥",FatherId=2,TypeId = 2 },
                new AreaItem(){ Id = 8,Name = "六安",FatherId=2,TypeId = 2 },
                new AreaItem(){ Id = 9,Name = "蜀山区",FatherId=7,TypeId = 3 },
                new AreaItem(){ Id = 10,Name = "经开区",FatherId=7,TypeId = 3 },
                new AreaItem(){ Id = 11,Name = "区1",FatherId=8,TypeId = 3 },
                new AreaItem(){ Id = 12,Name = "区2",FatherId=8,TypeId = 3 },
            };

        public ObservableCollection<DataItem> dataItems { get; set; } = new ObservableCollection<DataItem>();

        public ObservableCollection<AreaItem> proviceAreas { get; set; } = new ObservableCollection<AreaItem>();
        public ObservableCollection<AreaItem> cityAreas { get; set; } = new ObservableCollection<AreaItem>();
        public ObservableCollection<AreaItem> areaItems { get; set; } = new ObservableCollection<AreaItem>();


        private AreaItem? _selectProvice;
        public AreaItem? SelectProvice
        {
            get { return _selectProvice; }
            set
            {
                _selectProvice = value;
                cityAreas.Clear();
                if (value != null)
                {
                    foreach (var item in _areas.Where(it => it.FatherId == value.Id && it.TypeId == 2))
                    {
                        cityAreas.Add(item);
                    }
                    SelectCity = cityAreas[0];
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectCity"));

                }
            }
        }

        private AreaItem? _selectCity;

        public event PropertyChangedEventHandler? PropertyChanged;

        public AreaItem? SelectArea { get; set; }


        public AreaItem? SelectCity
        { 
            get => _selectCity;
            set
            { 
                _selectCity = value;
                areaItems.Clear();
                if (value != null)
                {
                    foreach (var item in _areas.Where(it => it.FatherId == value.Id && it.TypeId == 3))
                    {
                        areaItems.Add(item);
                    }
                    SelectArea = areaItems[0];
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectArea"));
                }
                
            }
        }

        public DataSource()
        {
            dataItems.Add(new DataItem() { Id = 1, Name = "张三", Description = "11111" });
            dataItems.Add(new DataItem() { Id = 2, Name = "李四", Description = "22222" });
            dataItems.Add(new DataItem() { Id = 3, Name = "王五", Description = "33333" });

            dataItems[0].Childen.Add(new DataItem() { Id = 4, Name = "张三111", Description = "44444" });
            dataItems[1].Childen.Add(new DataItem() { Id = 4, Name = "张三222", Description = "44444" });
            dataItems[2].Childen.Add(new DataItem() { Id = 4, Name = "张三222", Description = "44444" });



            

            Application.Current.Dispatcher.Invoke(() => 
            {
                (_areas.Where(it => it.TypeId == 1).ToList()).ForEach(el =>
                {
                    proviceAreas.Add(el);
                });

                SelectProvice = proviceAreas[0];
                SelectCity = cityAreas[0];
                SelectArea = areaItems[0];
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectProvice"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectCity"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectArea"));
            });
        }
    }

    public class DataItem
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public string? Description { get; set; }
        public List<DataItem> Childen { get; set; } = new();
    }

    public class AreaItem
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public int FatherId { get; set; }
        /// <summary>
        /// 1:省 2:市 3:区
        /// </summary>
        public int TypeId { get; set; }
    }
}
